package com.xzc.apitest.sinktest

;

import com.xzc.apitest.source.SensorReading
import com.xzc.apitest.transform.MyFilter
import org.apache.flink.api.common.serialization.SimpleStringEncoder
import org.apache.flink.core.fs.Path
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink
import org.apache.flink.streaming.api.scala._


object FileSink {
  def main(args: Array[String]): Unit = {
    //创建执行环境
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)

    //2-文件
    val stream2 = env.readTextFile("D:\\git\\learning_flink\\_01_试用\\src\\main\\resources\\sensor.txt")
    //    stream2.print()

    val dataStream = stream2
      .map(data => {
        val arr = data.split(",")
        SensorReading(arr(0), arr(1).toLong, arr(2).toDouble)
      })

    dataStream.print()
    //    dataStream.writeAsCsv("D:\\git\\learning_flink\\_01_试用\\src\\main\\resources\\sensor.csv")
    dataStream.addSink(
      StreamingFileSink.forRowFormat(
        new Path("D:\\git\\learning_flink\\_01_试用\\src\\main\\resources\\sensor1.csv"),
        new SimpleStringEncoder[SensorReading]()
      ).build())

    //执行
    env.execute("filesink test")
  }
}
